package org.dark.fund.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.dark.fund.api.UserService;
import org.dark.fund.api.dto.UserAddDTO;
import org.dark.fund.api.dto.UserDeleteDTO;
import org.dark.fund.exceptions.FundBizException;
import org.dark.fund.dao.dataobject.UserBaseInfo;
import org.dark.fund.dao.entity.User;
import org.dark.fund.dao.mapper.UserMapper;
import org.dark.fund.utils.EncryptUtil;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import static org.dark.fund.enums.ResultCode.*;

/**
 * @author <a href="mailto:chenxilzx1@gmail.com">theonefx</a>
 */
@Component
@Slf4j
public class UserServiceImpl implements UserService {
    private static final BeanCopier copier = BeanCopier.create(User.class, UserBaseInfo.class, false);
    @Resource
    private UserMapper userMapper;


    @Override
    public UserBaseInfo getUserBaseInfo(String userId) {
        User user = userMapper.selectById(userId);
        UserBaseInfo userBaseInfo = new UserBaseInfo();
        copier.copy(user, userBaseInfo, null);
        return userBaseInfo;
    }

    @Override
    public boolean passwordCheck(String userId, String password) {
        User user = userMapper.selectById(userId);
        if (null == user) {
            log.info("user [{}], login failed. no user registered", userId);
            return false;
        }
        String cipherPassword = user.getUserPassword();
        boolean result = StrUtil.equals(password, EncryptUtil.decrypt(cipherPassword));
        if (!result) {
            log.info("user [{}], login failed.", userId);
        }
        return result;
    }

    @Override
    public boolean registerUser(UserAddDTO userAddDTO) {
        User user = userMapper.selectById(userAddDTO.getUserId());
        if (null != user) {
            throw new FundBizException(USER_EXISTED);
        }
        String passwordCipher = EncryptUtil.encrypt(userAddDTO.getPassword());
        user = User.builder().userId(userAddDTO.getUserId()).userName(userAddDTO.getUsername())
                .userState("1").userTelephone(userAddDTO.getTelephone())
                .userPassword(passwordCipher)
                .usetRegisterTime(DateUtil.today())
                .build();
        boolean result = userMapper.insert(user) > 0;
        if (result) {
            log.info("注册用户[{}]成功,密码密文[{}].", userAddDTO.getUserId(), passwordCipher);
        } else {
            log.info("注册用户[{}]失败.", userAddDTO.getUserId());
        }
        return result;
    }

    @Override
    public boolean unregisterUser(UserDeleteDTO userDeleteDTO) {
        User user = userMapper.selectById(userDeleteDTO.getUserId());
        if (null == user) {
            throw new FundBizException(USER_NOT_EXISTED);
        }
        if (!passwordCheck(userDeleteDTO.getUserId(), userDeleteDTO.getPassword())) {
            throw new FundBizException(PASSWORD_CHECK_ERROR);
        }
        boolean result = userMapper.deleteById(user.getUserId()) > 0;
        if (result) {
            log.info("注销用户[{}]成功.", userDeleteDTO.getUserId());
        } else {
            log.info("注销用户[{}]失败.", userDeleteDTO.getUserId());
        }
        return result;
    }

    public static void main(String[] args) {
        String password = "wanglh,111";
        String ciphertext = EncryptUtil.encrypt(password);
        System.out.println("password: " + password + " , cip: " + ciphertext);
    }

}
